#include "LOGR_users.h"

extern const char *
LOGR_get_url_title(GraphFrame *gf,char *url);

WebUserPtr
new_web_user_node(void)
{
  WebUserPtr usn = (WebUserPtr)calloc(1, sizeof(WebUserStruct));
  if(!usn) {
      fprintf(stderr,"Out of Memory new_web_user_node\n");
      abort();
  }
  usn->lepages = init_linked_list();
  usn->cpath = new_web_path_node();
  LOGR_set_path_info(usn->cpath,(void *)usn);
  usn->ltimes = init_linked_list();
  return usn;
}

void
delete_web_user_node(WebUserPtr usn)
{	
    if(!usn) return;

    if(usn->uname) {
	free(usn->uname); usn->uname = NULL;
    }

    if(usn->referer) {
	free(usn->referer); usn->referer = NULL;
    }
    
    if(usn->epage) {
	free(usn->epage); usn->epage = NULL;
    }
    if(usn->ltimes)
	Delete_all_list(usn->ltimes); 

    if(usn->lepages)
	Delete_all_list(usn->lepages); 
    
    /*if(usn->cpath) {
	delete_web_path_node(usn->cpath); usn->cpath = NULL;
	}*/

    free(usn); usn = NULL;
}

void
LOGR_set_user_naccesses(WebUserPtr usn,long num)
{
  if(!usn) return;
  usn->naccesses = num;
}

void
LOGR_inc_user_naccesses(WebUserPtr usn)
{
  if(!usn) return;
  usn->naccesses++;
}

long
LOGR_get_user_naccesses(WebUserPtr usn)
{
  if(!usn) return 0;
  return usn->naccesses;
}

void
LOGR_set_user_name(WebUserPtr usn,char *name)
{
  if(!usn) return;
  GRStrAllocCopy(usn->uname,name);
}

char *
LOGR_get_user_name(WebUserPtr usn)
{
  if(!usn) return 0;
  return usn->uname;
}

void
LOGR_set_user_start_time(WebUserPtr usn,time_t stime)
{
  if(!usn) return;
  usn->startt = stime;
}

time_t
LOGR_get_user_start_time(WebUserPtr usn)
{
  if(!usn) return 0;
  return usn->startt;
}

void
LOGR_set_user_end_time(WebUserPtr usn,time_t etime)
{
  if(!usn) return;
  usn->endt = etime;
}

time_t
LOGR_get_user_end_time(WebUserPtr usn)
{
  if(!usn) return 0;
  return usn->endt;
}

void
LOGR_set_user_info(WebUserPtr usn,void * info)
{
  if(!usn) return;
  usn->info = info;
}

void *
LOGR_get_user_info(WebUserPtr usn)
{
  if(!usn) return 0;
  return usn->info;
}

void
LOGR_set_user_referer(WebUserPtr usn,char *referer)
{
  if(!usn) return;
  GRStrAllocCopy(usn->referer,referer);
}

void
LOGR_delete_user_referer(WebUserPtr usn)
{
  if(!usn) return;
  if(usn->referer) {
      free(usn->referer); usn->referer = NULL;
  }	
}

char *
LOGR_get_user_referer(WebUserPtr usn)
{
  if(!usn) return 0;
  return usn->referer;
}

void
LOGR_set_user_entry_page(WebUserPtr usn,char *entry_page)
{
  if(!usn) return;
  GRStrAllocCopy(usn->epage,entry_page);
}

void
LOGR_delete_user_entry_page(WebUserPtr usn)
{
  if(!usn) return;
  if(usn->epage) {
      free(usn->epage); usn->epage = NULL;
  }	
}

char *
LOGR_get_user_entry_page(WebUserPtr usn)
{
  if(!usn) return 0;
  return usn->epage;
}

const char *
LOGR_get_user_entry_page_title(WebUserPtr usn,GraphFrame *gf)
{
  if(!usn) return 0;
  return LOGR_get_url_title(gf,usn->epage);
}

void
LOGR_set_user_ltimes(WebUserPtr usn,LNode_type * ltimes)
{
  if(!usn) return;
  usn->ltimes = ltimes;
}

void
LOGR_add_user_time(WebUserPtr usn,void *time)
{
  if(!usn) return;
  if(usn->ltimes)
      Insert_linked_list(usn->ltimes,create_new_node_llist((char *)time));   
}

LNode_type *
LOGR_get_user_ltimes(WebUserPtr usn)
{
  if(!usn) return 0;
  return usn->ltimes;
}

void
LOGR_set_user_lepages(WebUserPtr usn,LNode_type * lepages)
{
  if(!usn) return;
  usn->lepages = lepages;
}

void
LOGR_add_user_entry_page(WebUserPtr usn,void *gen_page)
{
  if(!usn) return;
  if(usn->lepages)
      Insert_linked_list(usn->lepages,create_new_node_llist((char *)gen_page));   
}

LNode_type *
LOGR_get_user_lepages(WebUserPtr usn)
{
  if(!usn) return 0;
  return usn->lepages;
}

WebPathPtr
LOGR_get_user_current_path(WebUserPtr usn)
{
    if(!usn)
	return NULL;
    return usn->cpath;
}

void
LOGR_set_user_current_path(WebUserPtr usn,WebPathPtr cpath)
{
    if(!usn)
	return;
    usn->cpath = cpath;
}

WebUserPtr 
LOGR_new_web_user_info(char *dns, char *urls, char *urlt)
{
    WebUserPtr nup = NULL;	
    nup = new_web_user_node();
    LOGR_set_user_name(nup,dns);
    LOGR_set_user_referer(nup,urls);
    LOGR_set_user_entry_page(nup,urlt);
    LOGR_inc_user_naccesses(nup);
    return nup;
}


